package observermall

import "fmt"

// Observer 观察者接口
type Observer interface {
	// Update 接收通知
	Update(product *Product, eventType string, msg string)
	// GetID 获取观察者ID
	GetID() string
}

// 具体观察者

// EmailNotificationObserver 邮件通知观察者
type EmailNotificationObserver struct {
	UserID      string
	Email       string
	Preferences map[string]bool // 事件类型 -> 是否接收
}

func NewEmailNotificationObserver(userID, email string) *EmailNotificationObserver {
	return &EmailNotificationObserver{
		UserID: userID,
		Email:  email,
		Preferences: map[string]bool{
			EventTypePriceDrop: true,
			EventTypeReplenish: true,
		},
	}
}

func (o *EmailNotificationObserver) Update(product *Product, eventType string, msg string) {
	if o.Preferences[eventType] {
		// 模拟发送邮件
		fmt.Printf("发送邮件到 %s: 商品 %s - %s - %s\n", o.Email, product.Name, eventType, msg)
	}
}

func (o *EmailNotificationObserver) GetID() string {
	return o.UserID
}

// SMSNotificationObserver 短信通知观察者
type SMSNotificationObserver struct {
	UserID      string
	Phone       string
	Preferences map[string]bool // 事件类型 -> 是否接收
}

func NewSMSNotificationObserver(userID, phone string) *SMSNotificationObserver {
	return &SMSNotificationObserver{
		UserID: userID,
		Phone:  phone,
		Preferences: map[string]bool{
			EventTypeOutOfStock: true,
			EventTypeReplenish: true,
		},
	}
}

func (o *SMSNotificationObserver) Update(product *Product, eventType string, msg string) {
	if o.Preferences[eventType] {
		// 模拟发送短信
		fmt.Printf("发送短信到 %s: 商品 %s - %s - %s\n", o.Phone, product.Name, eventType, msg)
	}
}

func (o *SMSNotificationObserver) GetID() string {
	return o.UserID
}
